/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * The Class Notification.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public abstract class Notification implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 40L;

	/** The Constant TAG_CODE_CONTEXT. */
	public static final String TAG_CODE_CONTEXT = "{context}";

	/** The Constant TAG_CODE_PROJECT. */
	public static final String TAG_CODE_PROJECT = "{project}";

	/** The Constant TAG_CODE_NAME. */
	public static final String TAG_CODE_NAME = "{name}";

	/** The Constant TAG_CODE_PATH. */
	public static final String TAG_CODE_PATH = "{path}";

	/** The Constant TAG_CODE_START_TIMESTAMP. */
	public static final String TAG_CODE_START_TIMESTAMP = "{start_timestamp}";

	/** The tags map. */
	private final Map<String, String> tagsMap;

	/** The subject. */
	private String subject;

	/** The text. */
	private String text;

	/**
	 * Instantiates a new notification.
	 *
	 * @param notification the notification
	 */
	public Notification(Notification notification) {
		this.subject = notification.getSubject();
		this.text = notification.getText();

		this.tagsMap = new HashMap<String, String>();
	}

	/**
	 * Instantiates a new notification.
	 *
	 * @param subject the subject
	 * @param text the text
	 */
	public Notification(String subject, String text) {
		this.subject = subject;
		this.text = text;

		this.tagsMap = new HashMap<String, String>();
	}

	/**
	 * Gets the subject.
	 *
	 * @return the subject
	 */
	public String getSubject() {
		return this.subject;
	}

	/**
	 * Sets the subject.
	 *
	 * @param subject the new subject
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * Gets the text.
	 *
	 * @return the text
	 */
	public String getText() {
		return this.text;
	}

	/**
	 * Sets the text.
	 *
	 * @param text the new text
	 */
	public void setText(String text) {
		this.text = text;
	}

	/**
	 * Sets the tag.
	 *
	 * @param key the key
	 * @param value the value
	 */
	public void setTag(String key, String value) {
		this.tagsMap.put(key, value);
	}

	/**
	 * Sets the tag.
	 *
	 * @param key the key
	 * @param value the value
	 */
	public void setTag(String key, Date value) {
		final SimpleDateFormat dateFormat = new SimpleDateFormat();
		this.tagsMap.put(key, dateFormat.format(value));
	}

	/**
	 * Sets the tag.
	 *
	 * @param key the key
	 * @param value the value
	 */
	public void setTag(String key, Number value) {
		final DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###,###,##0.##");
		this.tagsMap.put(key, decimalFormat.format(value));
	}

	/**
	 *
	 * @param key
	 *
	 * @return
	 */
	public String getTag(String key) {
		return this.tagsMap.get(key);
	}

	/**
	 *
	 * @return
	 */
	protected String getFilledText() {
		//TODO Compatibility with 3.1.0 - Remove before 3.2 or 4.0
		return fillTextWithTags(this.text);
	}

	/**
	 *
	 * @return
	 */
	protected String getFilledSubject() {
		//TODO Compatibility with 3.1.0 - Remove before 3.2 or 4.0
		return fillTextWithTags(this.subject);
	}

	private String fillTextWithTags(String textContent) {

		String result = textContent;

		for (String key : this.tagsMap.keySet()) {
			String value = this.tagsMap.get(key);
			if (value == null) {
				value = "";
			}
			result = result.replace(key, value);
		}

		return result;
	}

	/**
	 * Remove tags.
	 */
	protected void removeTags() {
		this.tagsMap.clear();
	}

	/**
	 * Send.
	 *
	 * @return true, if successful
	 */
	public abstract boolean send();
}
